package com.ganjiangps.huochetou.service.impl;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ganjiangps.huochetou.common.util.StringUtil;
import com.ganjiangps.huochetou.mapper.PersonalManagementMapper;
import com.ganjiangps.huochetou.mapper.PersonalManagementNoMapper;
import com.ganjiangps.huochetou.mapper.PersonalManagementNoQuoteMapper;
import com.ganjiangps.huochetou.mapper.PersonalManagementUserMapper;
import com.ganjiangps.huochetou.model.PersonalManagementNo;
import com.ganjiangps.huochetou.model.PersonalManagementNoQuote;
import com.ganjiangps.huochetou.service.PersonalManagementNoService;

/**
 * 大小名单匹配Service
 * 
 * @author xiaoy
 *
 */
@Service
public class PersonalManagementNoServiceImpl extends ServiceImpl<PersonalManagementNoMapper, PersonalManagementNo>
		implements PersonalManagementNoService {

	@Resource
	PersonalManagementNoQuoteMapper personalManagementNoQuoteMapper;
	@Resource
	PersonalManagementUserMapper personalManagementUserMapper;

	@Resource
	PersonalManagementMapper personalManagementMapper;

	@Override
	// 显示小名单个数和总人数
	public List<PersonalManagementNo> listPersonalManagementNo() {
		// TODO Auto-generated method stub

		List<PersonalManagementNo> list = baseMapper.listPersonalManagementNo();

		for (PersonalManagementNo personalManagementNo : list) {

			int size = personalManagementNoQuoteMapper.selectCount(
					new EntityWrapper<PersonalManagementNoQuote>().eq("parentno", personalManagementNo.getPmno()));
			personalManagementNo.setSonsize(size);
			int num = personalManagementUserMapper.listUsers(personalManagementNo.getPmno()).size();
			personalManagementNo.setNum(num);

		}

		return list;
	}

	@Override
	public List<PersonalManagementNo> getPersonalManagementNo() {
		// TODO Auto-generated method stub
		return baseMapper.getPersonalManagementNo();
	}

	@Override
	@Transactional
	public boolean insert(PersonalManagementNo entity) {
		// TODO Auto-generated method stub

		entity.setAddtime(new Date());

		super.insert(entity);

		String[] nos = entity.getNos();

		String[] pmnames = entity.getPmnames();

		boolean flag = false;

		for (int i = 0; i < nos.length; i++) {
			if (nos[i] != null && pmnames[i] != null) {
				PersonalManagementNoQuote quote = new PersonalManagementNoQuote();
				quote.setParentNo(entity.getPmno());
				quote.setPmno(nos[i]);
				quote.setPmname(pmnames[i]);
				flag = quote.insert();

			}
		}
		return flag;

	}

	@Override
	@Transactional
	public boolean deleteById(Serializable id) {
		// TODO Auto-generated method stub
		PersonalManagementNo personalManagementNo = baseMapper.selectById(id);

		personalManagementNoQuoteMapper
				.delete(new EntityWrapper<PersonalManagementNoQuote>().eq("parentNo", personalManagementNo.getPmno()));

		return super.deleteById(id);
	}

	@Override
	public PersonalManagementNo selectById(Long id) {
		return baseMapper.selectById(id);
	}

	@Override
	@Transactional
	public boolean updateById(PersonalManagementNo entity) {

		entity.setAddtime(new Date());

		// TODO Auto-generated method stub
		super.updateById(entity);

		personalManagementNoQuoteMapper
				.delete(new EntityWrapper<PersonalManagementNoQuote>().eq("parentNo", entity.getPmno()));

		String[] nos = entity.getNos();
		String[] pmnames = entity.getPmnames();

		boolean flag = false;

		for (int i = 0; i < nos.length; i++) {
			if (StringUtil.isNotEmpty(nos[i]) && StringUtil.isNotEmpty(pmnames[i])) {
				PersonalManagementNoQuote quote = new PersonalManagementNoQuote();
				quote.setParentNo(entity.getPmno());
				quote.setPmname(pmnames[i]);
				quote.setPmno(nos[i]);
				flag = quote.insert();

			}

		}
		return flag;
	}

}
